/* eslint-disable no-unused-vars */
import namespace from "./namespace";
import extend from "lodash/extend";
import { getAngle } from "./util";
import Snap from "snapsvg";
/**
 * 旋转角标
 */
class RotateHandle extends namespace.shape.Circle {
  NAME = `${namespace.namespace}.RotateHandle`;
  /**
   * owner 角标的所属图形
   */
  constructor(attr) {
    super();

    this.isHandleFigures = true;
    this.selectable = false;
    this.mouseDownX = 0;
    this.mouseDownY = 0;

    this.setterWhitelist = extend(this.setterWhitelist, {
      owner(owner) {
        return this.setOwner(owner);
      }
    });

    super.attr(
      extend(
        {
          bgColor: "#0087ff",
          stroke: 0.5,
          width: 8,
          height: 8,
          color: "#0087ff"
        },
        attr
      )
    );
  }

  createShapeElement() {
    const shape = super.createShapeElement();
    shape.attr({ cursor: "col-resize" });
    return shape;
  }

  setOwner(owner) {
    this.owner = owner;
  }

  onDragStart(x, y) {
    this.mouseDownX = x;
    this.mouseDownY = y;
    this.oldAngle = this.owner.getRotationAngle();
  }

  onDrag(dx, dy, dx2, dy2, pos) {
    const { cx, cy } = this.owner.shape.getBBox();
    const { x, y } = pos;
    const angle = getAngle(cx, cy, this.mouseDownX, this.mouseDownY, x, y);
    this.owner.setRotationAngle(this.oldAngle + angle);
  }

  onDragEnd() {
    this.mouseDownX = 0;
    this.mouseDownY = 0;
  }

  show(canvas) {
    this.setCanvas(canvas);
    this.canvas.handleFigures.add(this);
    this.shape.insertAfter(this.owner.getShapeElement());
    this.repaint();

    return this;
  }

  hide() {
    this.canvas.handleFigures.remove(this);
    this.setCanvas(null);
    return this;
  }
}

namespace.RotateHandle = RotateHandle;
